﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LSBLL;
using LSEntity;
using LSUtility;
using OralExamBLL;
using OralExamEntity;

namespace LSWeb.Areas.OralExam.Controllers
{
    [OEAuthorizeTeacher]
    public class TeacherController : ControllerCommon<OEControllerFormat<OETeacherControllerFormatBLL, OETeacher.Common>, OETeacherControllerCheck, OETeacher.Common>
    {
        //
        // GET: /OralExam/Teacher/

        public ActionResult Index()
        {
            var list = new TestGetEntity
                           {
                               CurriculumId = Member.MemberGetCanUserCurriculum(),
                               SchoolId = Member.MemberExtend.SchoolId
                           }.TestGetList();
            ViewBag.MyClass = Member.ClassList;
            return View(new OETeacher.Index
                            {
                                TestList = list,
                            });
        }

        [HttpPost]
        public ActionResult Index(TestClassEntity model)
        {
            model.CreatorId = Member.AccountId;
            var r = model.TestSetClass();
            return Content(ResultMessage.ResultMessageJson(r ? 1 : 0, r ? "成功" : "失败"));
        }

        public ActionResult GetClassSet(int testId)
        {
            var result =
                (TestBLL.TestGetClassSet(testId, Member.AccountId) ?? new TestClassEntity()).ObjToJson(
                    "yyyy-MM-dd HH:mm");
            return Content(result);
        }

        public ActionResult Preview()
        {
            return View(new OETeacher.Preview
                            {
                                QuestionOT = new QuestionOT
                                                 {
                                                     QuestionTypeList = QuestionBLL.QuestionTypeForOTGetList(10014),
                                                     QuestionForOTList =
                                                         QuestionBLL.QuestionForOTGetList(10014)
                                                                    .QuestionForOTListSetGroup()
                                                 }
                            });
        }

        public ActionResult TestResult(int testId, int classId)
        {
            return View(new OETeacher.TestResult
                            {
                                TestId = testId,
                                ClassId = classId,
                            });
        }

        [HttpPost]
        public ActionResult TestResult(OETeacher.TestResult arge)
        {
            var cache = OECache<TestStepEntity>.MemoryListGet(OEWebConfig.TestStepCacheKey) ??
                        new List<TestStepEntity>();

            var testSteps = cache.Where(m => m.TestId == arge.TestId &&
                                             m.ClassId == arge.ClassId).ToList();

            var html = testSteps.ListFormatTable(WebPageConfig.ViewPath + "Temp/Teacher/TestResult.cshtml");
            return Content(html.ToHtmlString());
        }

        public ActionResult TestBegin()
        {
            return View(new OETeacher.TestBegin
                            {
                                TestClass =
                                    TestBLL.TestGetClassGetList(Member.ClassList.Select(m => m.ClassId).ToList()),
                            });
        }

        [HttpPost]
        public ActionResult TestBegin(int classId, int testId)
        {
            MemberBLL.MemberGetList(classId)
                     .Where(m => m.Type == MemberTypeEntity.Student)
                     .ToList()
                     .ForEach(m => OECache<TestStepEntity>.TestStepSetCache(m, testId));
            return Content(ResultMessage.ResultMessageJson(1, "OK"));
        }
    }
}
